package com.roger.tool.json;

import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import com.roger.tool.utils.SGDebug;

public class KJSONObject extends JSONObject {

	private KJSONObject() {
		super();
	}

	private KJSONObject(String json) throws JSONException {
		super(json);
	}

	public static KJSONObject createJsonObject(String json) {
		try {
			return new KJSONObject(json);
		} catch (JSONException e) {
			e.printStackTrace();
			SGDebug.w(e.toString());
			return null;
		}
	}

	public static KJSONObject createJsonObject() {
		return new KJSONObject();
	}

	@Override
	public Object get(String name) {
		try {
			return super.get(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return null;
		}
	}

	public Object get(String name, Object defaultValue) {
		try {
			return super.get(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return defaultValue;
		}
	}

	@Override
	public boolean getBoolean(String name) {
		try {
			return super.getBoolean(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return false;
		}
	}

	public boolean getBoolean(String name, boolean defaultValue) {
		try {
			return super.getBoolean(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return defaultValue;
		}
	}

	@Override
	public double getDouble(String name) {
		try {
			return super.getDouble(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return 0.0d;
		}
	}

	public double getDouble(String name, double defaultValue) {
		try {
			return super.getDouble(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return defaultValue;
		}
	}

	@Override
	public int getInt(String name) {
		try {
			return super.getInt(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return 0;
		}
	}

	public int getInt(String name, int defaultValue) {
		try {
			return super.getInt(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return defaultValue;
		}
	}

	@Override
	public KJSONArray getJSONArray(String name) {
		try {
			return KJSONArray.createJsonArray(super.getJSONArray(name).toString());
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return null;
		}
	}

	public KJSONArray getJSONArray(String name, KJSONArray defaultValue) {
		try {
			return KJSONArray.createJsonArray(super.getJSONArray(name).toString());
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return defaultValue;
		}
	}

	@Override
	public KJSONObject getJSONObject(String name) {
		try {
			return new KJSONObject(super.getJSONObject(name).toString());
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return null;
		}
	}

	public KJSONObject getJSONObject(String name, KJSONObject defaultValue) {
		try {
			return new KJSONObject(super.getJSONObject(name).toString());
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return defaultValue;
		}
	}

	@Override
	public long getLong(String name) {
		try {
			return super.getLong(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return 0;
		}
	}

	public long getLong(String name, long defaultValue) {
		try {
			return super.getLong(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return defaultValue;
		}
	}

	@Override
	public String getString(String name) {
		try {
			return super.getString(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return null;
		}
	}

	public String getString(String name, String defaultValue) {
		try {
			return super.getString(name);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return defaultValue;
		}
	}

	@Override
	public KJSONObject put(String name, boolean value) {
		try {
			return (KJSONObject) super.put(name, value);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return null;
		}
	}

	@Override
	public KJSONObject put(String name, double value) {
		try {
			return (KJSONObject) super.put(name, value);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return null;
		}
	}

	@Override
	public KJSONObject put(String name, int value) {
		try {
			return (KJSONObject) super.put(name, value);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return null;
		}
	}

	@Override
	public KJSONObject put(String name, long value) {
		try {
			return (KJSONObject) super.put(name, value);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return null;
		}
	}

	@Override
	public KJSONObject put(String name, Object value) {
		try {
			return (KJSONObject) super.put(name, value);
		} catch (Exception e) {
			SGDebug.w(e.toString());
			return null;
		}
	}

	@Override
	public Iterator keys() {
		return super.keys();
	}

	@Override
	public String toString() {
		return super.toString();
	}

}
